package ac.technion.StreamMania;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 *  An adapter for an Iconified Text List.  holds and manages a list of
 *   IconifiedText items, sets the view for each item.
 */
public class IconifiedTextListAdapter extends BaseAdapter {

	/** Remember our context so we can use it when constructing views. */
	private Context mContext;

	private List<IconifiedText> mItems = new ArrayList<IconifiedText>();

	/**constructor.
	 * @param context - the current context
	 */
	public IconifiedTextListAdapter(Context context) {
		mContext = context;
	}

	/**adds item to the list
	 * @param it - Iconified text to be added to the list.
	 */
	public void addItem(IconifiedText it) { mItems.add(it); }

	/**
	 * sets the entire list of iconifiedtext items
	 * @param lit -  the list of iconified text items to be used
	 */
	public void setListItems(List<IconifiedText> lit) { mItems = lit; }

	/** @return The number of items in the list */
	public int getCount() { return mItems.size(); }

	/**
	 * gets the item in the list that is in the specified position.
	 * @param position - the position of the requested item
	 */
	public Object getItem(int position) { return mItems.get(position); }

	public boolean areAllItemsSelectable() { return false; }

	/**
	 * checks if the position on the screen which was clicked is selectable.
	 * @param position - the position picked up by the touchscreen
	 * @return - whether the position is selectable or not.
	 */
	public boolean isSelectable(int position) { 
		try{
			return mItems.get(position).isSelectable();
		}catch (IndexOutOfBoundsException aioobe){
			return this.isSelectable(position);
		}
	}


	/** Use the array index as a unique id. */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * @param position - The current position chosen
	 * @param convertView - The old view to overwrite, if one is passed
	 * @param parent - The parentView of this view
	 * @returns a IconifiedTextView that holds wraps around an IconifiedText */
	public View getView(int position, View convertView, ViewGroup parent) {
		IconifiedTextView btv;
		
		if (convertView == null) {
			btv = new IconifiedTextView(mContext, mItems.get(position));
		} else { // Reuse/Overwrite the View passed
			// We are assuming(!) that it is castable! 
			btv = (IconifiedTextView) convertView;
		//	btv.setBackgroundColor(0x00FF0000);
			btv.setText(mItems.get(position).getText());
			btv.setIcon(mItems.get(position).getIcon());
		}
		return btv;
	}
}